<template>
    <div class="table-search-container" v-if="props.search.length > 0">
        <el-form ref="tableSearchRef" :model="state.form" size="default" label-width="100px" class="table-form">
            <el-row class="flex-x-end">
                <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" v-for="(val, key) in search" :key="key"
                        v-show="key === 0 || state.isToggle">
                    <template v-if="val.type !== ''">
                        <el-form-item
                                :label="val.label"
                                :prop="val.prop"
                                :rules="[{ required: val.required, message: `${val.label}不能为空`, trigger: val.type === 'input' ? 'blur' : 'change' }]"
                        >
                            <el-input v-model="state.form[val.prop]" :placeholder="val.placeholder" clearable
                                      v-if="val.type === 'input'" style="width: 100%"/>
                            <el-date-picker
                                    v-model="state.form[val.prop]"
                                    type="date"
                                    :placeholder="val.placeholder"
                                    v-else-if="val.type === 'date'"
                                    style="width: 100%"
                            />
                            <el-select v-model="state.form[val.prop]" :placeholder="val.placeholder"
                                       v-else-if="val.type === 'select'" style="width: 100%">
                                <el-option v-for="item in val.options" :key="item.value" :label="item.label"
                                           :value="item.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </template>
                </el-col>
                <!---->
                <el-form-item class="table-form-btn" :label-width="search.length <= 1 ? '10px' : '100px'">
                    <template #label v-if="search.length > 1">
                        <div class="table-form-btn-toggle ml10" @click="state.isToggle = !state.isToggle">
                            <span>{{ state.isToggle ? '收起筛选' : '展开筛选' }}</span>
                            <el-icon class="ml5">
                                <ArrowUp v-if="state.isToggle"/>
                                <ArrowDown v-if="!state.isToggle"/>
                            </el-icon>
                        </div>
                    </template>
                    <div>
                        <el-button size="default" type="primary" icon="Search" @click="onSearch(tableSearchRef)">查询</el-button>
                        <el-button size="default" type="info"  icon="Refresh" class="ml10" @click="onReset(tableSearchRef)"> 重置</el-button>
                    </div>
                </el-form-item>
            </el-row>
        </el-form>
    </div>
</template>

<script setup name="HSearch">

    /** prop */
    const props = defineProps({
        search: {
            type: Array,
            default: () => [],
        },
    });

    // 定义子组件向父组件传值/事件
    const emit = defineEmits();

    // 定义变量内容
    const tableSearchRef = ref()
    const state = reactive({
        form: {},
        isToggle: false,
    })

    onMounted(() => {
        initFormField();
    })

    // 查询
    const onSearch = (formEl) => {
        if (!formEl) return;
        formEl.validate((valid) => {
            if (valid) {
                emit('search', state.form);
            } else {
                return false;
            }
        });
    }

    // 重置
    const onReset = (formEl) => {
        if (!formEl) return;
        formEl.resetFields();
        emit('search', state.form);
    }

    // 初始化 form 字段，取自父组件 search.prop
    const initFormField = () => {
        if (props.search.length <= 0) return false;
        props.search.forEach((v) => (state.form[v.prop] = ''));
    }
</script>

<style scoped lang="less">
    .table-search-container {
        display: flex;
        .table-form {
            flex: 1;
            .table-form-btn-toggle {
                white-space: nowrap;
                user-select: none;
                display: flex;
                align-items: center;
                color: #409EFF;
            }
        }
    }
</style>
